from django.shortcuts import render

# Create your views here.
from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response

from secret.models import SecretModel
from secret.serializers import SecretModelSerializer


class SecretGenericAPIView(GenericAPIView):
    queryset = SecretModel.objects.all()
    serializer_class = SecretModelSerializer

    def get(self, request):
        try:
            target_name = request.GET.get('name')
            queryset = SecretModel.objects.filter(name=target_name)
            serializer = SecretModelSerializer(queryset, many=True)
            return Response(serializer.data, status=status.HTTP_200_OK)
        except Exception as e:
            print(str(e))
            return Response(status=status.HTTP_404_NOT_FOUND)

    def post(self, request):
        serializer = SecretModelSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
